<template>
  <el-popover placement="right" width="600" trigger="click" @show="open" ref="popover">
    <el-input v-model="name" slot="reference" />

    <el-input
      placeholder="请输入内容"
      v-model="queryParams.condition.name"
      size="small"
      clearable
    >
      <template slot="append"
        ><el-button @click="()=>{query(1)}" size="small">搜索</el-button></template
      >
    </el-input>
    <el-tabs
      class="pageTab"
      v-model="newsTabCodeActive"
      type="card"
      @tab-click="tabClick"
    >
      <el-tab-pane
        v-for="(tab, index) in newsTabCodeArr"
        :label="tab.name"
        :name="tab.name"
        :key="index"
      ></el-tab-pane>
    </el-tabs>

    <edu-table
      ref="tableList"
      :data="tableList"
      :pageSize="queryParams.pageSize"
      :pageNum="queryParams.pageNum"
      :total="queryParams.total"
      @page-change="handleCurrentChange"
      @rowClick="rowClick"
      :order="true"
      :selection="false"
    >
      <el-table-column label="标题" prop="title" />
        <el-table-column label="作者" prop="author" />
        <el-table-column label="所属分类" prop="serviceCatgoryName" />
        <el-table-column label="所属品类" prop="produceCategoryName" />
    </edu-table>

    <div class="flex buttons">
      <el-button @click="close" size="small">关闭</el-button>
      <el-button @click="submit" type="primary" size="small">确认</el-button>
    </div>
  </el-popover>
</template>

<script>
import {  pageNewsInfo, listNewsTab, } from "@/api/news";
import utils from "@/utils";

export default {
  props: ["value"],
  data() {
    return {
      name: '',
      newsTabCode: "", //新闻页签
      newsTabCodeActive: "", //新闻页签
      newsTabCodeArr: [], //新闻页签
      queryParams: {
        condition: {
          publishStatus:1
        },
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      selectvalue: "",
      formData: {
        name: "",
      },
      tableList: [],
      listNewsTab: listNewsTab,
    };
  },
  created() {
    this.initOptions();
    this.name = this.valueText
  },
  watch: {
    'valueText'(newValue, oldValue) {
      this.name = newValue
    }
  },
  methods: {
    open() {
      this.query(1);
    },
    close() {
      let popover = this.$refs.popover
      popover.doClose()
    },
    handleCurrentChange(page) {
      this.queryParams.pageSize = page.pageSize;
      this.queryParams.pageNum = page.pageNum;

      this.query();
    },
    async initOptions() {
      // 新闻页签
      let res3 = await listNewsTab();
      this.newsTabCodeArr = res3.returnObj;
      // 设置初始显示
      if (this.newsTabCodeArr.length > 0) {
        this.newsTabCode = res3.returnObj[0];
        this.newsTabCodeActive = res3.returnObj[0].name;
      }

      // 查询列表
      this.query();
    },
    // 新闻tab切换
    tabClick(tab, event) {
      this.newsTabCode = this.newsTabCodeArr[tab.index];

      this.query(1);
    },
    // 查询
    async query(pageNum) {
      // 有pageNum 重置为第一页
      if(pageNum){
        this.queryParams.pageNum = pageNum
      }
        
      this.queryParams.condition.newsTabCode = this.newsTabCode.code;
      let { returnObj } = await pageNewsInfo(this.queryParams);
      this.tableList = returnObj.list;
      this.queryParams.total = returnObj.total;
      this.queryParams.pageSize = returnObj.pageSize;
    },
    // 获取农产品id
    async rowClick(row) {
      this.formData = row;
    },
    async submit() {
      this.close();

      this.name = this.formData.title
      this.$emit("input", this.formData.id);
    },
  },
};
</script>

<style lang="scss" scoped>
.buttons {
  justify-content: flex-end;
}
</style>
